﻿using System.Configuration;
using System.Xml.Linq;

namespace DVK.ConstantsGenerator
{
    public class GeneratorHelper
    {
        private static string _propertyString = "\t\tpublic static readonly string {0} = \"{1}\";";
        private static string _constantFieldString = "\t\tpublic const string {0} = \"{1}\";";
        private static string _namespaceString = "namespace {0} {{";
        private static string _classString = "\tpublic static class {0} {{";
        private static string _enumString = "\tpublic enum {0} {{";
        private static string _endTag = "}";
        private static string _regionString = "\t\t#region {0}";
        private static string _regionEndString = "\t\t#endregion";
        private static string _summaryString = string.Format("{{0}}{0}\n{{0}}{1}\n{{0}}{2}",
            "/// <summary>",
            "/// {1}",
            "/// </summary>");

        public static string GetPropertyString(string name, string value)
        {
            return string.Format(_propertyString, name.Replace("_", ""), value);
        }

        public static string GetConstantFieldString(string name, string value)
        {
            return string.Format(_constantFieldString, name.Replace("_", ""), value);
        }

        public static string GetNamespaceString(string namespaceName)
        {
            return string.Format(_namespaceString, namespaceName);
        }

        public static string GetClassString(string className)
        {
            return string.Format(_classString, className);
        }

        public static string GetEnumString(string enumName)
        {
            return string.Format(_enumString, enumName);
        }

        public static string GetEndString()
        {
            return _endTag;
        }

        public static string GetRegionString(string regionName)
        {
            return _regionString.Replace("{0}", regionName);
        }

        public static string GetRegionEndString()
        {
            return _regionEndString;
        }

        private const string _dictionaryFieldStringFormat = "public static Dictionary<{0},{1}> {2} = new Dictionary<{0},{1}>();";
        public static string GetDictionaryFieldString(string keyType, string valueType, string fieldName)
        {
            return string.Format(_dictionaryFieldStringFormat, keyType, valueType, fieldName);
        }

        public static string GetStaticConstructorString(string className)
        {
            return "\t\tstatic " + className + "() {";
        }

        public static string FormatString(string unformatted)
        {
            return unformatted.Replace("-", "").Replace(" ", "");
        }

        /// <summary>
        /// Gets the summary.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static string GetSummary(string propertyName)
        {
            return GetSummary(propertyName, 2);
        }

        /// <summary>
        /// Gets a summary with the specified number of tabs.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="tabCount">The tab count.</param>
        /// <returns></returns>
        public static string GetSummary(string content, int tabCount)
        {
            string tabs = new string('\t', tabCount);
            return string.Format(_summaryString, tabs, content);
        }

        public static string GetPathFromAppSettings(string name)
        {
            return ConfigurationManager.AppSettings[name];
        }

        public static string ParseName(string name)
        {
            int index = -1;
            while ((index = name.IndexOf(" ")) != -1)
            {
                string temp = name.Substring(index, 2);
                name = name.Replace(temp, temp.ToUpper());
                name = name.Remove(index, 1);
            }
            name = name.Substring(0, 1).ToUpper() + name.Remove(0, 1);
            name = name.Replace("-", "");

            return name;
        }

        public static XName GetName(string name)
        {
            return XName.Get(name, "http://schemas.microsoft.com/sharepoint/");
        }
    }

}
